package com.jsrj.controller;

import com.jsrj.common.annotation.Log;
import com.jsrj.common.core.controller.BaseController;
import com.jsrj.common.core.domain.AjaxResult;
import com.jsrj.common.core.page.TableDataInfo;
import com.jsrj.common.enums.BusinessType;
import com.jsrj.common.utils.poi.ExcelUtil;
import com.jsrj.domain.Advertorial;
import com.jsrj.domain.ImgAdv;
import com.jsrj.service.IAdvertorialService;
import com.jsrj.service.IImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 【救援信息】Controller
 *
 * @author ruoyi
 * @date 2022-09-03
 */

@RestController
@RequestMapping("/system/advertorial")
public class AdvertorialController extends BaseController
{
    @Autowired
    private IAdvertorialService advertorialService;

    @Autowired
    private IImageService imageService;
    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:advertorial:list')")
    @GetMapping("/list")
    public TableDataInfo list(Advertorial advertorial)
    {
        startPage();
        List<Advertorial> list = advertorialService.selectAdvertorialList(advertorial);
        return getDataTable(list);
    }
    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:advertorial:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Advertorial advertorial)
    {
        List<Advertorial> list = advertorialService.selectAdvertorialList(advertorial);
        ExcelUtil<Advertorial> util = new ExcelUtil<Advertorial>(Advertorial.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }
    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:advertorial:query')")
    @GetMapping(value = "/{title}")
    public AjaxResult getInfo(@PathVariable("title") String title)
    {
        return AjaxResult.success(advertorialService.selectAdvertorialByTitle(title));
    }


    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:advertorial:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{titles}")
    public AjaxResult remove(@PathVariable String[] titles)
    {
        return toAjax(advertorialService.deleteAdvertorialByTitles(titles));
    }


    /**
     * 添加软文
     * @param advertorial
     * @return
     */
    @PostMapping("/insert")
    public AjaxResult insert(@RequestBody Advertorial advertorial){
        if (advertorial.getInsertTime()==null){
            advertorial.setInsertTime(new Date());
        }
        return toAjax(advertorialService.insertAdvertorial(advertorial));
    }

    @PostMapping("/update")
    public AjaxResult update(@RequestBody Advertorial advertorial){
        return toAjax(advertorialService.updateAdvertorial(advertorial));
    }

    @GetMapping("/delete")
    public AjaxResult delete(@RequestParam Long id){
        return toAjax(advertorialService.deleteAdver(id));
    }



    /**
     * 根据标题，内容，关键字模糊查询
     * vague:模糊
     */
    @GetMapping("/query")
    public AjaxResult queryVague(String vague){
        List<Advertorial> advertorials = advertorialService.queryVague(vague);
        return AjaxResult.success(advertorials);
    }




    @GetMapping("/advImg")
    public AjaxResult queryImgAdv(@RequestParam Long id){

        String url = imageService.selectIdBuyUrl(id);
        Advertorial advertorial = advertorialService.queryAdvByImageId(id);

        ImgAdv imgAdv = new ImgAdv();
        imgAdv.setImageUrl(url);
        imgAdv.setTitle(advertorial.getTitle());
        imgAdv.setId(advertorial.getId());
        imgAdv.setImageUrl(advertorial.getImageUrl());
        imgAdv.setDescription(advertorial.getDescription());

        return AjaxResult.success(imgAdv);
    }

    /**
     * 查询全部
     * @return
     */
    @GetMapping("/selectAll")
    public TableDataInfo selectAll(){
        startPage();
        return getDataTable(advertorialService.queryAllAdvertorials());
    }
    @GetMapping("/selectById")
    public AjaxResult queryAdvById(@RequestParam Long id){
        startPage();
        return AjaxResult.success(advertorialService.queryAdvById(id));
    }







}
